package com.vanchu.dropdownview.component;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.vanchu.dropdownview.Dp2Px;

import java.util.List;

/**
 * Created by grant on 17-1-3.
 */
public class ListDropDownView<T> extends DropDownView {

    private FrameLayout parentView;

    private FrameLayout containerView;

    private ListView listView;

    private Config config;

    private ListDropDownView(View anchorView, Config config) {
        super(anchorView);
        this.config = config;
        initParentView(anchorView);
        initContainerView(anchorView);
        initListView(anchorView);
        setWindowContentView(parentView);
    }

    private void initParentView(View anchorView) {
        parentView = new FrameLayout(anchorView.getContext());
        parentView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        parentView.setBackgroundResource(config.bgRes);
        parentView.setPadding(
                (int) Dp2Px.dp2px(parentView.getContext(), config.parentPadding.left),
                (int) Dp2Px.dp2px(parentView.getContext(), config.parentPadding.top),
                (int) Dp2Px.dp2px(parentView.getContext(), config.parentPadding.right),
                (int) Dp2Px.dp2px(parentView.getContext(), config.parentPadding.bottom)
        );
    }

    private void initContainerView(View anchorView) {
        containerView = new FrameLayout(anchorView.getContext());
        containerView.setLayoutParams(new FrameLayout.LayoutParams(config.containerWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        parentView.addView(containerView);
    }

    private void initListView(View anchorView) {
        listView = new ListView(anchorView.getContext());
        listView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        containerView.addView(listView);
    }

    private <T> void setupListView(final DropItemAdapter<T> dropItemAdapter, final ItemClickListener<T> itemClickListener) {
        this.listView.setAdapter(dropItemAdapter);
        this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!itemClickListener.onItemClick(position, dropItemAdapter.getDatas().get(position))) {
                    ListDropDownView.this.close();
                }
            }
        });
    }

    public static class Builder<T> {
        private View anchorView;
        private Config config;
        private DropItemAdapter<T> dropItemAdapter;
        private ItemClickListener<T> itemClickListener;

        public Builder(View anchorView) {
            this.anchorView = anchorView;
        }

        public void setConfig(Config config) {
            this.config = config;
        }

        public void setDropItemAdapter(DropItemAdapter<T> dropItemAdapter) {
            this.dropItemAdapter = dropItemAdapter;
        }

        public void setItemClickListener(ItemClickListener<T> itemClickListener) {
            this.itemClickListener = itemClickListener;
        }

        public ListDropDownView create() {
            ListDropDownView listDropDownView = new ListDropDownView(anchorView, config);
            listDropDownView.setupListView(dropItemAdapter, itemClickListener);
            return listDropDownView;
        }
    }

    public interface ItemClickListener<T> {
        /**
         * @param position 位置
         * @param data     数据
         * @return true 点击后不关闭弹窗 反之返回 false
         */
        boolean onItemClick(int position, T data);
    }

    public abstract static class DropItemAdapter<T> extends BaseAdapter {

        private List<T> datas;

        List<T> getDatas() {
            return datas;
        }

        protected DropItemAdapter(List<T> datas) {
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        @SuppressLint("ViewHolder")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            convertView = layoutInflater.inflate(getItemLayoutId(), null);
            bind(convertView, datas.get(position));
            return convertView;
        }

        public abstract int getItemLayoutId();

        public abstract void bind(View itemView, T data);
    }

    public static class Config {
        private int bgRes;

        private int containerWidth;

        private Padding parentPadding;

        public Config(int bgRes, int containerWidth, Padding parentPadding) {
            this.bgRes = bgRes;
            this.containerWidth = containerWidth;
            this.parentPadding = parentPadding;
        }

        public static class Padding {

            private int left;
            private int top;
            private int right;
            private int bottom;

            public Padding(int left, int top, int right, int bottom) {
                this.left = left;
                this.top = top;
                this.right = right;
                this.bottom = bottom;
            }
        }
    }
}